<template>
  <div class="">
    <el-form :inline="true">
      <div class="path-top-box">
        <span @click="tapPath(-1)">根目录</span>
        <span v-for="(item,index) in fullpathStr" :key="index" @click="tapPath(index)">{{ item }}</span>
      </div>
    </el-form>
    <el-table v-loading="listLoading" :span-method="objectSpanMethod" :data="tableData" border style="width: 100%">
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.type=='0'">
            <div style="text-align: left;" class="path-box" @click="goPath(scope.row)">
              {{ scope.row.fullpath }}
            </div>
          </template>
          <template v-else slot-scope="scope">
            <div v-if="scope.row.type==0">文件</div>
            <div v-if="scope.row.type=='1'" class="imgbox" @click="handleSuccess(scope.row.webpath)">
              <img :src="scope.row.webpath" alt="" title="点击预览">
            </div>
            <div v-if="scope.row.type=='2'">
              <i style="font-size: 45px;" class="el-icon-video-camera" />
            </div>
            <div v-if="scope.row.type=='3'">
              <i style="font-size: 45px;" class="el-icon-tickets" />
            </div>
            <div v-if="scope.row.type=='4'">
              <i style="font-size: 45px;" class="el-icon-folder" />
            </div>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="文件名" align="center" />
      <el-table-column prop="length" label="大小" align="center" />
      <el-table-column label="操作" width="160px" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.webpath" size="mini" type="primary" @click="download(scope.row)">下载</el-button>
          <el-button size="mini" type="danger" @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="visibleType=='1'?'新增分类':'修改分类'"
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @closed="handleClose"
    >
      <div>
        <el-form ref="forms" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="typename">
            <el-input v-model="form.typename" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item label="分类编码" prop="typecode">
            <el-input v-model="form.typecode" placeholder="请输入分类编码" />
          </el-form-item>
          <el-form-item label="分类描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入分类描述" />
          </el-form-item>
          <el-form-item label="启用/禁用" prop="state">
            <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEdit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="dialogImg">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { getToken, getFiles, setFiles } from '@/utils/auth'
import { mapState } from 'vuex'

import { GetTypes, AddEditType, DeleteType } from '@/api/news.js'
import { GetAllFiles, DeleteFiles } from '@/api/ctrls.js'
import ipConfig from '../../../../ip-config'
export default {
  name: 'Name',
  data() {
    return {
      pathArr: [], // 存放点击路径
      path: getFiles() || '', // 访问路径

      dialogImageUrl: '',
      dialogImg: false,
      form: {},

      listLoading: false,
      tableData: [],
      PathData: [], // 路径列表
      FilesData: [], // 文件列表
      totalCount: 0,
      PageIndex: 1,
      dialogVisible: false,
      visibleType: '',
      rules: {
        title: [
          { required: true, message: '请输入配置说明', trigger: 'blur' }
        ],
        rechargeamount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        inamount: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请选择排序', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    fullpathStr() {
      const a = this.path.split('\\')
      return a
    }
  },
  watch: {
    PageIndex(val) {
      console.log(val)
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    tapPath(idx) {
      const a = this.fullpathStr.slice(0, idx + 1)
      this.path = a.join('\\')
      setFiles(this.path)
      this.getData()
    },
    goPath(item) {
      this.path = item.fullpath
      setFiles(this.path)
      this.getData()
    },
    download(item) {
      window.open(item.webpath)
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 没有地址 表示为文件目录
      if (row.type == '0') {
        if (columnIndex === 0) {
          return {
            rowspan: 1,
            colspan: 4
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    setFrom() {
      this.form = {
        idx: 0,
        typename: '',
        typecode: '',
        description: '',
        state: 1
      }
    },
    // 上传成功
    quillImgSuccess({ success, data }, file) {
      this.form.picurl = data
      console.log(this.form.picurl)
    },

    // 删除
    handleRemove({ status, url }, file) {
      this.form.picurl = ''
    },
    // 预览
    handleSuccess(file) {
      this.dialogImageUrl = file
      this.dialogImg = true
    },
    async onDelete(item) {
      try {
        const confirm = await this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm == 'confirm') {
          const { success, msg } = await DeleteFiles({
            path: item.fullpath
          })
          this.getData()
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        }
      } catch (e) {
        // 取消
        console.log(e)
      }
    },
    // 状态
    async SetValid(val, item, type) {
      const { success, msg } = await RechargediscountSetState({
        state: val
      }, [item.id])
      if (success) {
        this.getData()
      }
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    onEdit() {
      this.$refs['forms'].validate(async(valid) => {
        if (valid) {
          const { success, msg } = await AddEditType(this.form)
          if (success) {
            this.dialogVisible = false
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        } else {
          return false
        }
      })
    },
    // 关闭前
    handleClose() {
      this.$refs.forms.resetFields()
    },
    // 打开前
    onVisible(type, item = {}) {
      this.visibleType = type
      this.setFrom()
      this.form = { ...this.form, ...item }
      this.dialogVisible = true
    },
    async onSearch() {},
    async getData(address) {
      this.listLoading = true
      const { data: { dirslist, fileslist }} = await GetAllFiles({
        path: this.path
      })
      this.FilesData = fileslist
      this.PathData = dirslist
      this.tableData = [...dirslist, ...fileslist]
      this.listLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>
  .path-top-box {
    padding-bottom: 20px;

    span {
      color: blue;
      cursor: pointer;
    }

    span~span {
      &::before {
        content: '\\';
        display: inline-block;
        padding: 0 10px;
      }
    }
  }

  .path-box {
    color: blue;
    text-decoration: underline;
    cursor: pointer;
  }

  .imgbox {
    text-align: center;

    img {
      width: 60px;
      height: 60px;
      object-fit: cover;
      cursor: pointer;
    }
  }
</style>
<style lang="scss">
  .el-table .warning-row {
    background: oldlace;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 100%;
    display: block;
  }
</style>
